#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words)
    {
        int n = words[0].size();
        int u = 0;
        vector<int> ans;
        vector<string> wordssort = words;
        ranges::sort(wordssort);
        while (n--)
        {
            string cpy(s);
            if (u != 0)
            {
                cpy.insert(cpy.begin() + u, ' ');
                for (int i = words[0].size() + u + 1; i < cpy.size(); i += words[0].size() + 1)
                    cpy.insert(cpy.begin() + i, ' ');
            }
            else
            {
                for (int i = words[0].size(); i < cpy.size(); i += words[0].size() + 1)
                    cpy.insert(cpy.begin() + i, ' ');
            }
            stringstream ss(cpy);
            string a;

            vector<string> hash;
            if (u != 0)
            {
                int left = u, right = u;
                while (ss >> a)
                {
                    if (a.size() < words[0].size())
                        continue;
                    hash.push_back(a);
                    right += words[0].size();
                    if (hash.size() < words.size())
                        continue;
                    vector<string> hashsort = hash;
                    sort(hashsort.begin(), hashsort.end());
                    if (hashsort == wordssort)
                        ans.push_back(left);
                    hash.erase(hash.begin());
                    left += words[0].size();
                }
            }
            else
            {
                int left = 0, right = 0;
                while (ss >> a)
                {
                    hash.push_back(a);
                    right += words[0].size();
                    if (hash.size() < words.size())
                        continue;
                    vector<string> hashsort = hash;
                    ranges::sort(hashsort);
                    if (hashsort == wordssort)
                        ans.push_back(left);
                    hash.erase(hash.begin());
                    left += words[0].size();
                }
            }
            u++;
        }
        return ans;
    }
};